package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.VO.SubOptionVO;
import com.gibs.peixunapi.VO.SubjectHubVO;
import com.gibs.peixunapi.dao.SubjectHubDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.excepiton.BaseException;
import com.gibs.peixunapi.model.SubjectHub;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.SubOptionService;
import com.gibs.peixunapi.service.SubjectHubService;
import com.gibs.peixunapi.utils.ConverterUtil;
import com.gibs.peixunapi.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SubjectHubServiceImpl implements SubjectHubService {

    @Autowired
    private SubjectHubDao subjectHubDao;
    @Autowired
    private SubOptionService subOptionService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result create(SubjectHubVO object) {
        try {
            SubjectHub subjectHub = new SubjectHub();
            ConverterUtil.copyNotBlankProperties(object, subjectHub);
            SubjectHub result = subjectHubDao.saveAndFlush(subjectHub);
            if (!object.getOptionVOList().isEmpty()) {
                subOptionService.create(object.getOptionVOList(), result);
            }
            return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException(ResultEnum.CREATE_FAIL);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result save(SubjectHubVO object) {
        try {
            SubjectHub subjectHub = subjectHubDao.findById(object.getId()).orElseThrow(EntityNotFoundException::new);
            ConverterUtil.copyNotNullProperties(object, subjectHub);
            subjectHubDao.save(subjectHub);
            if (object.getOptionVOList() != null && (object.getOptionVOList().size() >= 1)) {
                subOptionService.save(object.getOptionVOList());
            }
            ConverterUtil.copyNotNullProperties(object, subjectHub);
            subjectHubDao.save(subjectHub);
            return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    @Override
    public Result delete(Integer id) {
        try {
//            String[] ids = id.split(",");
//            for (String str : ids) {
//                subjectHubDao.delete(Integer.valueOf(str));
//            }
            return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }



    @Override
    public Result getListPage(String key, Integer current, Integer limit) {
        try {
//            List<SubjectHub> list = new ArrayList<>();
//            limit = limit == null ? 10 : limit;
//            current = current == null ? 1 : current;
//            int offset = (current - 1) * limit;
//            Long total = null;
//            if (!StringUtils.isEmpty(key)) {
//                list = subjectHubDao.findPageLike("%" + key + "%", offset, limit);
//                total = subjectHubDao.countLike("%" + key + "%");
//            } else {
//                list = subjectHubDao.findPage(offset, limit);
//                total = subjectHubDao.count();
//            }
//            if (total == 0) {
//                PageResult(true, Status.OK, "无数据", new ArrayList(), 0L, 0, 0);
//            }
//            List<Object> data = new ArrayList<>();
//            for (SubjectHub object : list) {
//                HashMap<String, Object> map = new HashMap();
//                map.put("id", object.getId());
//                map.put("questionId", object.getQuestionId() == null ? "" : object.getQuestionId());
//                HashMap<String, Object> questionTypeId = new HashMap<String, Object>();
//                questionTypeId.put("name", object.getQuestionTypeId() == null ? "" : object.getQuestionTypeId().getName());
//                questionTypeId.put("id", object.getQuestionTypeId() == null ? "" : object.getQuestionTypeId().getId());
//                map.put("questionTypeId", questionTypeId);
//                map.put("level", object.getLevel() == null ? "" : object.getLevel());
//                map.put("knowPoint", object.getKnowPoint() == null ? "" : object.getKnowPoint());
//                map.put("status", object.getStatus() == null ? "" : object.getStatus());
//                map.put("accuracy", object.getAccuracy() == null ? "" : object.getAccuracy());
//                data.add(map);
//            }
//            PageData pageData = new PageData(total, limit, current);
            return ResultUtil.successPage(ResultEnum.SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }


    @Override
    public Result getOptions(String key) {
        try {
//            List<SubjectHub> list = new ArrayList<>();
//            if (!StringUtils.isEmpty(key)) {
//                list = subjectHubDao.findLike("%" + key + "%");
//            } else {
//                list = subjectHubDao.findAll();
//            }
//            List<Object> data = new ArrayList<>();
//            for (SubjectHub object : list) {
//                HashMap<String, Object> map = new HashMap();
//                map.put("value", object.getId());
//                map.put("name", object.getQuestionId());
//                data.add(map);
//            }
            return ResultUtil.success(ResultEnum.SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result getVOListByIdList(List<Integer> idList) {
        List<SubjectHub> subjectHubList = subjectHubDao.findSubjectHubsByIdIn(idList);
        if (subjectHubList.isEmpty()){
            return ResultUtil.success(ResultEnum.DATA_NOT_FIND);
        }
        List<SubjectHubVO> subjectHubVOList = new ArrayList<>();
        subjectHubList.forEach(subjectHub->{
            SubjectHubVO temp = new SubjectHubVO();
            ConverterUtil.copyProperties(subjectHub,temp,"answer","analysis","knowPoint");
            List<SubOptionVO> subOptionVOList = subOptionService.getSubOptions(subjectHub.getId());
            if (!subjectHubList.isEmpty()){
                temp.setOptionVOList(subOptionVOList);
            }
            temp.setSort(subjectHubList.indexOf(subjectHub)+1);
            subjectHubVOList.add(temp);
        });
        return ResultUtil.success(ResultEnum.SUCCESS,subjectHubVOList);
    }

    @Override
    public Integer persist(SubjectHubVO object) {
        try {
            SubjectHub subjectHub = new SubjectHub();
            ConverterUtil.copyNotBlankProperties(object, subjectHub);
            SubjectHub result = subjectHubDao.saveAndFlush(subjectHub);
            if (!object.getOptionVOList().isEmpty()) {
                subOptionService.create(object.getOptionVOList(), result);
            }
            return result.getId();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException(ResultEnum.CREATE_FAIL);
        }
    }


}

